﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IRC.IRepairClouds.DataLayer
{
    [Serializable]
    public struct Settable<T>
    {
        private bool _isSet;
        private T _value;

        public static readonly Settable<T> NotSet = new Settable<T>();

        public Settable(T value)
        {
            this._value = value;
            this._isSet = true;
        }

        public bool IsSet
        {
            get { return this._isSet; }
        }

        public T Value
        {
            get
            {
                if (!this._isSet)
                    throw new InvalidOperationException("Object is not set");
                return this._value;
            }
        }

        public void Set(T value)
        {
            this._value = value;
            this._isSet = true;
        }

        public void Reset()
        {
            this._value = default(T);
            this._isSet = false;
        }

        public override string ToString()
        {
            if (!this._isSet)
                return "";
            return this._value.ToString();
        }

        public static implicit operator Settable<T>(T value)
        {
            return new Settable<T>(value);
        }

        public static explicit operator T(Settable<T> value)
        {
            return value.Value;
        }
    }
}
